/*
 *  Sphere.cpp
 *  rayTracer
 *
 *  Created by Zachry Thayer on 10/7/09.
 *  Copyright 2009 A_Nub. All rights reserved.
 *
 */

#include "Sphere.h"
#include <limits>

Intersect Sphere::intersect(Ray &ray){
	Vec3 dst = getPosition() - ray.src;
	float B = dst.dot(ray.dir);
	float C = B*B - dst.dot(dst) + (getScale().x*getScale().x);
	if(C < 0) return Intersect();
	float D = sqrtf(C), E = B+D;
	if(E < 0) return Intersect();
	float F = B - D;
	float dist = (F > 0) ? F : E;
	dist -= 0.001f;
	Vec3 p = ray.src + (ray.dir * dist);
	Vec3 n = p - getPosition();
	n.normalize();
	float reflet = 2.0f * (ray.dir.dot(n));
	Vec3 r = ray.dir - (n * reflet);
	return Intersect(dist, -1, p, n, r, mat);
	
}

bool Sphere::inside(Vec3 &point){
	Vec3 dif = getPosition() - point;
	float radius = getScale().x;
	if( dif.dot(dif) < radius * radius)
		return true;
	return false;
}

